<template>
  <div>
    <nav-bar class="home-nav"><div slot="center">购物街</div></nav-bar>
    <tab-control ref="topTab" @tabClick="tabClick" v-show="showTabControl" class="tab-control"></tab-control>
    <scroll :probe-type="3"
            :pull-up-load="true"
            @pullingUp="loadMore"
            @scroll="contentScroll"
            ref="scroll">
      <swiper :imgs="banners" @swiperLoaded="swiperLoaded"></swiper>
      <recommend-view :recommend="recommends"></recommend-view>
      <FeatureView/>
      <tab-control ref="contentTab" @tabClick="tabClick" class="tab-control"></tab-control>
      <goods-list :goods="showGoodsList"></goods-list>
    </scroll>
    <back-top v-show="showBackTop" @click.native="backTop"/>
  </div>
</template>

<script>
  import NavBar from "components/common/navbar/NavBar"
  import Swiper from "components/common/swiper/Swiper"
  import RecommendView from "views/home/childComps/RecommendView"
  import FeatureView from "views/home/childComps/FeatureView"
  import TabControl from 'components/content/tabControl/TabControl'
  import GoodsList from 'components/content/goods/GoodsList'
  import Scroll from "components/common/scroll/Scroll"

  import {gerMultiData,getProductData} from "network/home";
  import {backTopMixin} from "common/mixin";
  import {TOP_DISTANCE} from "common/const";

  export default {
    name: "Home",
    data(){
      return{
        banners:[],
        recommends: [],
        goods: {
          'pop': {page: 1, list: []},
          'new': {page: 1, list: []},
          'sell': {page: 1, list: []},
        },
        currentType: 'pop',
        tabOffsetTop: 0,

        tabControlOffset: {pop:0,new:0,sell:0},      //用于流行  新款  精选切换时的位置记忆
        oldOffsetTop: 0,         //用于流行  新款  精选切换时的位置记忆
        showTabControl: false
      }
    },
    activated() {
      this.$refs.scroll.refresh()
      this.$refs.scroll.scrollTo(0, this.tabControlOffset[this.currentType], 0)
    },
    deactivated() {
      this.tabControlOffset[this.currentType] = this.$refs.scroll.scrollY
    },
    watch:{
      currentType(newType,oldType){
        this.tabControlOffset[oldType] = this.oldOffsetTop
        this.$refs.scroll.scrollTo(0,this.tabControlOffset[newType],0)
      }
    },
    mixins: [backTopMixin],
    components:{
      NavBar,
      Swiper,
      RecommendView,
      FeatureView,
      TabControl,
      GoodsList,
      Scroll
    },
    created() {
      gerMultiData().then(res=>{
        this.banners=res.data.banner.list;
        this.recommends = res.data.recommend.list
      })
      this.getGoodstData('pop')
      this.getGoodstData('new')
      this.getGoodstData('sell')

      this.$bus.$on('imgLoad', () => {
        this.$refs.scroll.refresh()
      })
    },
    methods:{
      contentScroll(position){
        this.showTabControl = -position.y >= this.tabOffsetTop
        this.showBackTop = -position.y >= TOP_DISTANCE
      },
      swiperLoaded() {
        this.tabOffsetTop = this.$refs.contentTab.$el.offsetTop;
      },
      loadMore(){
        this.getGoodstData(this.currentType)
      },
      tabClick(index){
        switch (index) {
          case 0:
            this.currentType = 'pop'
            break
          case 1:
            this.currentType = 'new'
            break
          case 2:
            this.currentType = 'sell'
            break
        }
        this.$refs.contentTab.currentIndex = index
        this.$refs.topTab.currentIndex = index

        this.oldOffsetTop = this.$refs.scroll.scrollY

      },

      getGoodstData(type){
        getProductData(type,this.goods[type].page).then(
            res=>{
              const newList = res.data.list
              this.goods[type].list.push(...newList)
              this.goods[type].page += 1

              this.$refs.scroll.finishedPullUp()
            }
        )
      }

    },
    computed:{
      showGoodsList() {
        return this.goods[this.currentType].list
      }
    }

  }
</script>

<style scoped>
  .home-nav{
    background-color: var(--color-tint);
    color: #fff;
  }
  .tab-control {
    background-color:#ffffff;
    position: relative;
    z-index: 9;
  }
</style>